package com.wangbo.box.builder;

/**
 * 此例子为okHttpClient的建造者模式
 *
 * 建造者（Builder）模式在应用过程中可以根据需要改变，如果创建的产品种类只有一种，只需要一个具体建造者，这时可以省略掉抽象建造者，甚至可以省略掉指挥者角色。
 *
 */
public class BuilderPattern {


    static class Subject {
        private int version;
        private String name;
        private String ext;

        @Override
        public String toString() {
            return "Subject{" +
                    "version=" + version +
                    ", name='" + name + '\'' +
                    ", ext='" + ext + '\'' +
                    '}';
        }

        Subject(Builder builder) {
            version = builder.version;
            name = builder.name;
            ext = builder.ext;
        }

        /**
         * 建造者
         */
        static final class Builder{
            int version;
            String name;
            String ext;

            public Builder() {
                version = 1;
            }

            public Builder buildName(String name){
                this.name = name;
                return this;
            }

            public Builder buildExt(String ext){
                this.ext = ext;
                return this;
            }

            public Subject build(){
                return new Subject(this);
            }
        }
    }

    public static void main(String[] args) {
        Subject subject = new Subject.Builder().buildExt("ext").buildName("name").build();
        System.out.println(subject);
    }

}
